from django.db import models


'''
1 有车型(CarModel)，车厂（CarFactory），经销商(Distributor)三个表,一个车厂可以生产多种车型，一个经销商可以出售多种车型，一个车型可以有多个经销商出售
  车型：车型名，车型出厂价，车厂id
  车厂：车厂名，车厂地址，联系电话
  经销商：经销商名，地址，联系电话
2 有用户表，基于django内置user表，扩展mobile字段
'''


class CarModel(models.Model):
    model_name = models.CharField(max_length=32,verbose_name='车型名')
    model_price = models.IntegerField(verbose_name='车型出厂价')
    carfactory = models.ForeignKey(to='CarFactory',on_delete=models.CASCADE)
    # 获取车厂名：
    def factory_name(self):
        return {'factory_name':self.carfactory.factory_name}
    # 获取经销商名和联系电话
    def distributor_name_phone(self):
        l = []
        for distributor_obj in self.distributor_set.all():
            l.append({'distributor_name':distributor_obj.distributor_name,'distributor_phone':distributor_obj.distributor_phone})
        return l


class CarFactory(models.Model):
    factory_name = models.CharField(max_length=32,verbose_name='车厂名')
    factory_addr = models.CharField(max_length=64,verbose_name='车厂地址')
    factory_phone = models.CharField(max_length=11,verbose_name='车厂联系电话')


class Distributor(models.Model):
    distributor_name = models.CharField(max_length=32,verbose_name='经销商名')
    distributor_addr = models.CharField(max_length=64,verbose_name='经销商地址')
    distributor_phone = models.CharField(max_length=11,verbose_name='经销商电话')
    carmodel = models.ManyToManyField(to='CarModel',through='CarModel2Distributor')

class CarModel2Distributor(models.Model):
    carmodel = models.ForeignKey(to='CarModel',on_delete=models.CASCADE)
    distributor = models.ForeignKey(to='Distributor',on_delete=models.CASCADE)


from django.contrib.auth.models import AbstractUser


class UserInfo(AbstractUser):
    mobile = models.CharField(max_length=11,verbose_name='用户电话')